<template>
  <div id="app">
    <router-view v-if="isRouterAlive"/>
  </div>
  <!-- <vue-route-transition id="app" :keepAlive="false"></vue-route-transition> -->
</template>

<script>
import { setToken, getToken, removeToken } from "@/utils/auth";
import { getUrl } from "@/utils";

export default {
  name: "App",
   provide () {
    return {
      reload: this.reload
    }
  },
  data() {
    return {
      isRouterAlive: true,
      code: "",
      appid: "",
      Secret: "",
      portrait: "",
      state: "" //state=workweixin是企业微信。state=weixin是服务号登录
    };
  },
  methods: {
    reload () {
      this.isRouterAlive = false
      this.$nextTick(function () {
        this.isRouterAlive = true
      })
    },
    //钉钉免登
    authSerivce: function() {
      let that = this;
      dd.ready(function() {
        dd.runtime.permission.requestAuthCode({
          corpId: localStorage.getItem("corpId"), //hmw
          onSuccess: function(result) {
            localStorage.setItem("code", result.code);
            that.wxUserData();
          },
          onFail: function(err) {
            if (err.status == 101) {
              Dialog.alert({
                message: "请联系管理员添加权限"
              }).then(() => {
                // on close
              });
            }
          }
        });
      });
    },
    // 获取用户信息
    wxUserData(){
      let app = localStorage.getItem("appid");
      let app1 = app.split("-");
      let appid = app1[0];
      let Secret = app1[1];
      let params = {}
      if (localStorage.getItem("sysStatus") == 1) {
        params = {
          code: localStorage.getItem("code"),
          appkey: appid,
          appsecret: Secret,
          company_id: localStorage.getItem("companyId"),
          sysStatus: localStorage.getItem("sysStatus")
        }
      } else if (localStorage.getItem("sysStatus") == 2) {
        params = {
          code: localStorage.getItem("code"),
          appkey: localStorage.getItem("appKey"),
          appsecret: localStorage.getItem("appSecret"),
          company_id: localStorage.getItem("companyId"),
          sysStatus: localStorage.getItem("sysStatus")
        }

      } else if (localStorage.getItem("sysStatus") == 3) {

      }

      let query = Object.assign({}, params);
      this.$store.dispatch('wxUserDataList', query).then(res => {
          localStorage.setItem("oss_public_url",'https://saic-public-2.oss-cn-shanghai.aliyuncs.com')
          if (res.token) {
              console.log('res.token: ', res.token);
            this.portrait = res.headimgurl;
            if (localStorage.getItem("sysStatus") == 1) {
              localStorage.setItem("headimgurl", res.headimgurl);
            }else if (localStorage.getItem("sysStatus") == 2) {
              localStorage.setItem("headimgurl", res.portrait);
            }
            localStorage.setItem("user", JSON.stringify(res.user));
            this.openid = res.openid;
            localStorage.setItem("openid", res.openid);
            localStorage.setItem("userId", res.userid);
            localStorage.setItem('token',res.token)
            setToken({ key: res.user.token_key, val: res.token });
            // if (res.company_id == 6) {
            //   this.$router.replace('/Home3')
            // } else if (res.company_id == 3) {
            //   this.$router.replace('/ajwl')
            // } else if(res.company_id == 4){
            //   this.$router.replace('/Home1')
            // }
            this.$router.replace('/Container')

          }else{

          }
          if (res.token == null || res.token == ''){
            localStorage.setItem("openid", res.openid);
            localStorage.setItem("headimgurl", res.headimgurl);
            localStorage.setItem("userId", res.userid);
            if (localStorage.getItem("sysStatus") == 1) {
              localStorage.setItem("headimgurl", res.headimgurl);
            }else if (localStorage.getItem("sysStatus") == 2) {
              localStorage.setItem("headimgurl", res.portrait);
            }
            this.portrait = res.headimgurl;
            localStorage.setItem('token',res.token)
            setToken({ key: 'reception_cookie_token', val: res.token });
            localStorage.setItem("user", JSON.stringify(res));
            this.openid = res.openid;
             window.localStorage.removeItem("token");
            removeToken();
            // window.localStorage.clear()
            this.$router.replace('/Login')
          }
      }).catch( error => {

      })
    }
  },
  created() {
    let user = {}
    if (localStorage.user) {
      user = JSON.parse(localStorage.getItem("user"))
      localStorage.setItem("companyId",user.company_id);
      localStorage.setItem("headimgurl",user.headimgurl||user.portrait);
      localStorage.setItem("userId",user.dingtalk_userid);
      localStorage.setItem("openid", user.openid);
    }

    localStorage.setItem("corpId", getUrl("corpId") || null);
    localStorage.setItem("appKey", getUrl("appKey") || null);
    localStorage.setItem("appSecret", getUrl("appSecret") || null);
    localStorage.setItem("appid", getUrl("appid") || null);
    localStorage.setItem("state", getUrl("state") || null);
    localStorage.setItem("source", getUrl("source") || null);
    localStorage.setItem("code", getUrl("code") || null);
    localStorage.setItem("companyId",getUrl("state")? getUrl("state").substr(getUrl("state").length-3,1) : user.company_id);
    localStorage.setItem("sysStatus", getUrl("state")? getUrl("state").substr(getUrl("state").length-1,1) : null);   // 1 微信  2 钉钉  3 企业微信

    if (localStorage.getItem("sysStatus") == 3) {
      this.EwxUserId();
    } else if (localStorage.getItem("sysStatus") == 1) {
      this.wxUserData();
    } else if (localStorage.getItem("sysStatus") == 2) {
      this.authSerivce();
    }

  },
  mounted() {

  }
};
</script>

<style>
#app {
  height: 100%;
  background: rgb(243, 248, 252);
}
</style>
